<template>
    <div class="baidu-search-list">
      <!-- 搜索区域 -->
      <div class="search-header">
        <a href="https://www.baidu.com" target="_blank">
          <img src="https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png" alt="百度 logo" class="logo" />
        </a>
        <el-form inline :model="searchForm" @submit.prevent="handleSearch">
          <el-form-item>
            <el-input
              v-model="searchForm.keyword"
              placeholder="请输入搜索关键词"
              style="width: 500px;"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">百度一下</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 搜索结果列表 -->
      <div class="search-results" v-if="searchResults.length">
        <div v-for="(result, index) in searchResults" :key="index" class="result-item">
          <h3 class="result-title">
            <a :href="result.url" target="_blank">{{ result.title }}</a>
          </h3>
          <p class="result-snippet">{{ result.snippet }}</p>
          <span class="result-url">{{ result.url }}</span>
        </div>
        <!-- 分页组件 -->
        <el-pagination
          :current-page="currentPage"
          :page-size="pageSize"
          :total="total"
          @current-change="handlePageChange"
          layout="prev, pager, next"
        />
      </div>
      <div v-else class="no-results" v-if="hasSearched">暂无搜索结果</div>
    </div>
  </template>
  
  <script setup lang="ts">
  import { reactive, ref } from 'vue';
  
  // 定义搜索表单数据
  const searchForm = reactive({
    keyword: ''
  });
  
  // 模拟搜索结果
  const searchResults = ref<any[]>([]);
  // 当前页码
  const currentPage = ref(1);
  // 每页显示数量
  const pageSize = ref(10);
  // 总结果数
  const total = ref(0);
  // 是否已经搜索过
  const hasSearched = ref(false);
  
  // 处理搜索逻辑
  const handleSearch = () => {
    const keyword = searchForm.keyword.trim();
    hasSearched.value = true;
    if (keyword) {
      // 模拟获取搜索结果
      simulateSearchResults(keyword);
    } else {
      searchResults.value = [];
      total.value = 0;
    }
  };
  
  // 模拟获取搜索结果
  const simulateSearchResults = (keyword: string) => {
    // 模拟请求延迟
    setTimeout(() => {
      const results = [];
      for (let i = 0; i < 50; i++) {
        results.push({
          title: `${keyword} 相关结果 ${i + 1}`,
          snippet: `这是关于 ${keyword} 的模拟搜索结果片段 ${i + 1}。`,
          url: `https://example.com/search?q=${encodeURIComponent(keyword)}&page=${i + 1}`
        });
      }
      searchResults.value = results.slice((currentPage.value - 1) * pageSize.value, currentPage.value * pageSize.value);
      total.value = results.length;
    }, 500);
  };
  
  // 处理分页变化
  const handlePageChange = (page: number) => {
    currentPage.value = page;
    handleSearch();
  };
  </script>
  
  <style scoped>
  .baidu-search-list {
    padding: 20px;
  }
  
  .search-header {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
  }
  
  .logo {
    width: 120px;
    height: 44px;
    margin-right: 20px;
  }
  
  .search-results {
    max-width: 800px;
  }
  
  .result-item {
    margin-bottom: 20px;
  }
  
  .result-title {
    font-size: 18px;
    margin-bottom: 5px;
  }
  
  .result-title a {
    color: #1a0dab;
    text-decoration: none;
  }
  
  .result-title a:hover {
    text-decoration: underline;
  }
  
  .result-snippet {
    color: #4d5156;
    font-size: 14px;
    line-height: 1.5;
    margin-bottom: 5px;
  }
  
  .result-url {
    color: #006621;
    font-size: 14px;
  }
  
  .no-results {
    color: #666;
    font-size: 16px;
  }
  </style>